package com.sky.controller.admin;

import com.sky.constant.RedisConstants;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("菜品新增")
    public Result<String> save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        // 将所有的菜品缓存数据清理掉，所有以cache:dish:开头的key
        cleanCache(RedisConstants.CACHE_DISH_KEY + dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("菜品删除")
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.batchDelete(ids);

        // 将所有的菜品缓存数据清理掉，所有以cache:dish:开头的key
        cleanCache("cache:dish:*");
        return Result.success();
    }

    @PutMapping
    @ApiOperation("菜品修改")
    public Result<String> updateWithDishFlavor(@RequestBody DishDTO dishDTO) {
        dishService.updateWithDishFlavor(dishDTO);

        // 将所有的菜品缓存数据清理掉，所有以cache:dish:开头的key
        cleanCache("cache:dish:*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品禁用启用")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        dishService.startOrStop(status, id);

        // 将所有的菜品缓存数据清理掉，所有以cache:dish:开头的key
        cleanCache("cache:dish:*");
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("菜品详情")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @GetMapping("/list")
    @ApiOperation("分类菜品列表")
    public Result<List<Dish>> getDishByCategory(Long categoryId) {
        log.info("根据分类查询菜品：{}", categoryId);
        List<Dish> dishes = dishService.list(categoryId);
        return Result.success(dishes);
    }

    /**
     * 清除缓存
     *
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
